﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using System.Data.Linq;

namespace DomainModel.Concrete
{
    // Implementation of the JobHistories section of the database.
    // The middle layer of the DAL.
    class SqlJobHistoriesRepository : IJobHistoriesRepository
    {
        private Table<JobHistory> jobHistoriesTable;

        public SqlJobHistoriesRepository(string connectionString)
        {
            jobHistoriesTable = (new DataContext(connectionString)).GetTable<JobHistory>();
        }

        public IQueryable<JobHistory> JobHistories
        {
            get { return jobHistoriesTable; }
        }

        public void SaveJobHistory(JobHistory jobHistory)
        {
            if (jobHistory.JobHistoryID == 0)
            {
                jobHistoriesTable.InsertOnSubmit(jobHistory);
            }
            else
            {
                jobHistoriesTable.Attach(jobHistory);
                jobHistoriesTable.Context.Refresh(RefreshMode.KeepCurrentValues, jobHistory);
            }

            jobHistoriesTable.Context.SubmitChanges();
        }

        public void DeleteJobHistory(JobHistory jobHistory)
        {
            jobHistoriesTable.DeleteOnSubmit(jobHistory);
            jobHistoriesTable.Context.SubmitChanges();
        }
    }
}